home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / 80X86 / DOS32V33.ZIP / LIB / RANDOM.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-10-26  |  2.1 KB  |  89 lines

  1. ;                  library file for DOS32 32bit DOS extender
  2. ;        Writen by Adam Seychell
  3.  
  4.  
  5. .386
  6. .Model flat, C
  7.  
  8. Public Random
  9. Public Randomize
  10. Public Random_Number
  11.  
  12. .CODE
  13.  
  14. comment $
  15. ╔═══════════════════════════════════════════════════════════════════════╗
  16. ║ Returns EAX with a random number with CL bits in size.                ║
  17. ║                                                                       ║
  18. ║   The algorithem was from an article in Doctor Dobbs Journal          ║
  19. ║   issue date  MAY 1991                                                ║
  20. ║                                                                       ║
  21. ║  NOTE: the initial random number is taken from the CMOS clock         ║
  22. ╚═══════════════════════════════════════════════════════════════════════╝$
  23.  
  24.  
  25.  
  26. Random PROC
  27.         push edx
  28.         push ebx
  29.         xor eax,eax
  30.         mov bl,byte ptr Random_Number
  31.         and bl,1
  32.  
  33. Gen_bit:    ; make n bit numbers
  34.         shl eax,1
  35.  
  36.     mov edx,Random_Number
  37.  
  38.         shr edx,9
  39.         xor bl,dl
  40.  
  41.         shr edx,5
  42.         xor bl,dl
  43.  
  44.         bt ebx,1
  45.     rcr Random_Number,1
  46.         setc bl
  47.     or  al,bl
  48.  
  49.     dec cl
  50.         jnz Gen_bit
  51.         pop ebx
  52.         pop edx
  53.     ret
  54. Random  Endp
  55.  
  56. align 4
  57. Random_Number     DD 0
  58.  
  59.  
  60. ;╔══════════════════════════════════════════════════════════════════════╗
  61. ;║ Get inital random number from CMOS time                ║
  62. ;╚══════════════════════════════════════════════════════════════════════╝
  63. Randomize PROC
  64.  
  65.         push    es
  66.         mov     ax,0EE00h               ; GET DOS32 selector values
  67.         int     31h
  68.         mov     es,bx
  69.  
  70.         mov     al,0                    ; Get seconds
  71.         out     70h,al
  72.         in      al,71h
  73.         shl     eax,8
  74.         mov     al,2                    ; Get minute
  75.         out     70h,al
  76.         in      al,71h
  77.         shl     eax,8
  78.         xor     eax,es:[046Ch]             ; throw in number of ticks
  79.         pop     es
  80.         not     eax
  81.         mov     Random_Number,eax
  82.         ret
  83.  
  84. Randomize ENDP
  85.  
  86. ;────────────────────────────────────────────────────────────────────────────
  87.  
  88.         End
  89.